package com.oocl.admin_platform.servlet;

import java.io.IOException;
import java.security.Provider.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.LogManager;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.oocl.admin_platform.pojo.FailReason;
import com.oocl.admin_platform.pojo.Merchant;
import com.oocl.admin_platform.service.FailReasonService;
import com.oocl.admin_platform.service.MerchantService;

import com.oocl.admin_platform.service.factory.ServiceFactory;

/**
 * Servlet implementation class TransStatesServlet
 */
public class ChangeStatusServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private MerchantService merchantService;
	private FailReasonService failReasonService;
	private static Logger logger = Logger.getLogger(ChangeStatusServlet.class);

	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		merchantService = ServiceFactory.getMerchantServiceInstance();
		failReasonService = ServiceFactory.getFailReasonServiceInstance();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String oldStatus = request.getParameter("oldStatus");
		String id = request.getParameter("id");
		String status = request.getParameter("status");
		String failReason = request.getParameter("failReason");

		Merchant merchant = merchantService.findById(id);
		merchant.setStatus(Integer.parseInt(status.trim()));
		boolean flag = merchantService.changeStatus(id, status);
		if (flag) {
			logger.info("success change the status " + merchant);
			if ("1".equals(status)) {
				response.sendRedirect("getMerchantList?action=search&status=1");
			} else if ("2".equals(status)) {
				response.sendRedirect("getMerchantList?action=search&status=2");
			} else if ("3".equals(status)) {
				if ("".equals(failReason) || failReason == null) {
					logger.error("not fill the fail reason");
					String error = "必填";
					request.setAttribute("error", error);
					request.getRequestDispatcher(
							"getMerchantDetails?id=" + merchant.getId()
									+ "&status=" + oldStatus).forward(request,
							response);
				} else {
					FailReason reason = new FailReason(null, merchant.getId(),
							failReason);
					if (failReasonService.findByMerchantId(id) != null) {
						failReasonService.update(reason);
					} else {
						failReasonService.save(reason);
					}

					response.sendRedirect("getMerchantList?action=search&status=3");

				}
			} else {
				logger.error("fail on change the status " + merchant);
				response.sendRedirect("getMerchantList?action=all");
			}
		}
	}

}
